//
//  HWGameDetailTradeController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/23.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXPagingView
class HWGameDetailTradeController: HWBaseViewController {
    var gameId = ""
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    var tableView:UITableView!
    let tableViewProtocol = HWGameDetailTradeProtocol()
    let httpManager = HWGameDetailHttpManager()
    var modelArr = NSMutableArray()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addAction()
        
        loadData()
        
        addRefresh()
    }
    
}
extension HWGameDetailTradeController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        self.view.backgroundColor = .white
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.emptyActionView(withImageStr: "", titleStr: "", detailStr: "暂无数据", btnTitleStr: "点击刷新", btnClick: {
            self.loadData()
        })
        tableView.ly_emptyView.actionBtnCornerRadius = 15*widthScale
        tableView.ly_emptyView.actionBtnHeight = 30*widthScale
        tableView.ly_emptyView.contentViewOffset = -120*widthScale
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
extension HWGameDetailTradeController:JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }
    
    func listScrollView() -> UIScrollView {
        return self.tableView
    }
    
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        self.listViewDidScrollCallback = callback
    }
    
}

extension HWGameDetailTradeController {
    func addAction() -> Void {
        tableViewProtocol.scrollDidScroll = {
            scrollView in
            self.listViewDidScrollCallback?(scrollView)
        }
        tableViewProtocol.clickItem = {
            let accountDetail = HWAccountDetailsViewController()
            accountDetail.idStr = $0
            self.navigationController?.pushViewController(accountDetail, animated: true)
        }
    }
}

extension HWGameDetailTradeController {
    func loadData() -> Void {
        tableView.mj_footer?.isHidden = true
        httpManager.getGameDetailTradeList(gameId: self.gameId) { models in
            self.tableView.mj_footer?.endRefreshing()
            self.modelArr.removeAllObjects()
            if models.count > 0 {
                self.tableView.mj_footer?.isHidden = false
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameDetailTradeModel]
            self.tableView.reloadData()
        } failure: { errorString in
            self.view.showHUD(title: errorString)
            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_footer?.endRefreshing()
        }
    }
    
    func addRefresh() -> () {
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
        tableView.mj_footer?.isHidden = true
    }
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getGameDetailTradeListFooter(gameId: self.gameId) { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameDetailTradeModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
    }
}

